package com.cqvie.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqvie.controller.msg.AttendDetailsMsg;
import com.cqvie.controller.msg.FindAttendMsg;
import com.cqvie.controller.msg.FindLeaveMsg;
import com.cqvie.controller.msg.FindStuMsg;
import com.cqvie.controller.msg.FindTByIDMsg;
import com.cqvie.controller.msg.SelectionCourseMsg;
import com.cqvie.controller.msg.Tmovecompetence;
import com.cqvie.controller.msg.UpdateStuMsg;
import com.cqvie.controller.msg.tloginMsg;
import com.cqvie.dao.AttendDao;
import com.cqvie.dao.AttendDetailsDao;
import com.cqvie.dao.CommonDao;
import com.cqvie.dao.CourseDao;
import com.cqvie.dao.CourseScheduleDao;
import com.cqvie.dao.LeaveMessageDao;
import com.cqvie.dao.StudentDao;
import com.cqvie.dao.TeacherDao;
import com.cqvie.model.Attend;
import com.cqvie.model.AttendDetails;
import com.cqvie.model.Course;
import com.cqvie.model.CourseSchedule;
import com.cqvie.model.LeaveMessage;
import com.cqvie.model.Role;
import com.cqvie.model.Student;
import com.cqvie.model.Teacher;

@Transactional
@Service("teacherService")
public class TeacherService {

	@Resource
	private TeacherDao teacherDao;
	@Resource
	private CourseScheduleDao csDao;
	@Resource
	private CourseDao courseDao;
	@Resource
	private StudentDao studentDao;
	@Resource
	private AttendDao attendDao;
	@Resource
	private LeaveMessageDao leaveMsgDao;
	@Resource
	private AttendDetailsDao adDao;

	@Resource
	private CommonDao commonDao;
	
	// 教师登录功能
	public boolean isExist(tloginMsg teacher,HttpSession session) {
		if (teacherDao.selectByTeacher(teacher.getTeacherID(),teacher.getPassword())) {
			System.out.println(" login sucess");
			session.setAttribute("jurisdiction",((Role)(commonDao.findObjectByHQL("FROM Role where description='teacher_master'"))).getName());
			session.setAttribute("username", teacher.getTeacherID());
			return true;
		}
		System.out.println(" login falid"); 
		return false;
	}

	// 教师选课功能;(查询所有属于该教师的课程)
	public List<Course> CourseSelection(SelectionCourseMsg sc) {
		List<Course> courses = new ArrayList<Course>();
		// 通过教师ID来获取课程ID
		List<CourseSchedule> courseID = csDao.SelectCourseID(sc.getTeacherId());
		for (CourseSchedule cs : courseID) {
			for (int i = 1; i <courseID.size(); i++) {
				//通过课程ID来获取课程
				List<Course> c = courseDao.selectByID(cs.getCourseID().getCourseID());
				courses.add(c.get(0));
			}
		}
		return courses;
	}
	
	//根据教师ID来获取教师的ID和Name(直接返回Teacher对象)
	public Teacher findTeacherByID(FindTByIDMsg ftb){
		return teacherDao.selectTeacherByID(ftb.getTeacherID());
	}

	// 查询学生信息功能
	public List<Student> findStudendts(FindStuMsg p) {
		List<Student> students;
		boolean result=Pattern.compile("^[0-9]{9,}$").matcher(p.getPam()).matches();
		if (result) {
			int stuid =Integer.parseInt(p.getPam());
			// 通过ID来查询学生
			students = studentDao.selectByStuID(stuid);
		} else {
			// 根据Name来查询学生
			students = studentDao.selectByStuName(p.getPam());
		}
		return students;
	}

	// 添加考勤记录
	public void saveAttend(Attend at) {
		attendDao.addAttendMessage(at);
	}
	
	//添加考勤明细
	public void saveAttendDateils(AttendDetailsMsg admsg){
		AttendDetails ad=new AttendDetails();
		ad.setStuID(admsg.getStuID());
		ad.setStuName(admsg.getStuName());
		ad.setAttendType(admsg.getAttendType());
		ad.setAttend(attendDao.GetAttend(admsg.getAttend_ID()));
		adDao.addAttendDeatils(ad);
	}

	// 添加请假记录,同时给该请假人所在请假时间段内的违纪记录打上请假的标记
	public void saveLeaveMsg(LeaveMessage lm) {
		String mark="true";
		//请假人的ID
		int leaveName=lm.getStudent().getStudentID();
		Date startT=lm.getStartDate();
		Date endT =lm.getEndDate();
		List<Attend> asList=attendDao.selectAll();
		for (Attend attend : asList) {
			Date attDate=attend.getAttendDate();
			if(attDate.getTime()>=startT.getTime()&&attDate.getTime()<=endT.getTime()){
				adDao.modifyByName(mark,  leaveName);
			}
		}
		leaveMsgDao.addMsg(lm);
	}

	// 查询指定学生的考勤信息
	public List<AttendDetails> findstuAttendDateils(FindAttendMsg fam) {
		List<AttendDetails> attends = adDao.selectBystuName(fam.getStuName());
		if (attends!=null) {
			return attends;
		} else
			return null;
	}

	// 查询指定学生的请假信息(根据ID查询)
	//type int leavename
	public List<LeaveMessage> findstuLeaveMsg(FindLeaveMsg flm) {
		List<LeaveMessage> lms = leaveMsgDao.selectByIDLeaveMsg(flm.getLeavename());
		if (lms!=null) {
			return lms;
		} else
			return null;
	}
	
	//转移权限给指定的学生
	public boolean moveCopmpetrnce(Tmovecompetence tc){
		boolean f=csDao.updateCSstuID(tc.getStuID(),tc.getT_id(),tc.getC_id());
		return f;
	}
	
	//修改学生信息
	public boolean updateStu(UpdateStuMsg stuMsg){
		return studentDao.updatestuClass(stuMsg.getStuclassName(),stuMsg.getStuID());
	}
}
